{extend name="common"}

{block name="content"}
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
    <legend>用户管理</legend>
</fieldset>

<form class="layui-form" action="" method="">
    <div class="layui-form-item" style="width: 80%">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="username" required lay-verify="username" autocomplete="off" placeholder="请输入用户名"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item" style="width: 80%">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input type="password" name="password" required lay-verify="password" autocomplete="off" placeholder="请输入密码"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item" style="width: 80%">
        <label class="layui-form-label">请重复密码</label>
        <div class="layui-input-block">
            <input type="password" name="repass" required lay-verify="repass" autocomplete="off" placeholder="请重复密码"
                   class="layui-input" >
        </div>
    </div>

    <div class="layui-form-item" style="width: 80%">
        <label class="layui-form-label">邮箱</label>
        <div class="layui-input-block">
            <input type="text" name="email" required lay-verify="email" autocomplete="off" placeholder="请输入邮箱"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item" style="width: 80%">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input type="text" name="name" required lay-verify="name" autocomplete="off" placeholder="请输入姓名"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item" style="width: 80%">
        <label class="layui-form-label">权限</label>
        <div class="layui-input-block">
            <select name="perm" id="perm" >
                <option value="">请选择权限</option>
                <option value="3">管理员</option>
                <option value="0">设计</option>
                <option value="2">工程</option>
                <option value="1">商务</option>
                <option value="4">财务</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="adduser">添加</button>
        </div>
    </div>
</form>

{/block}

{block name="script"}
<script>
    layui.use(['form'], function () {
        var form = layui.form;
        var $ = layui.jquery;

        form.verify({
//            username: function (value) {
//                if (!/^[0-9_a-zA-Z]{6,12}$/.test(value) || value == '') {
//                    return "请输入6到12位数字、字母！"
//                }
//            },
//            password: function (value) {
//                if (!/^[\S]{6,12}$/.test(value) || value == '') {
//                    return "请输入6到12位密码！"
//                }
//            },
//            repass: function (value) {
//                if (!/^[\S]{6,12}$/.test(value) || value == '') {
//                    return "请输入6到12位密码！"
//                }
//            },
            email: function (value) {
                if (!/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(value) || value == '') {
                    return "请输入正确邮箱"
                }
            },
            name: function (value) {
                if (!/^[\S]{1,15}$/.test(value) || value == '') {
                    return "请输入姓名！"
                }
            },
        });

        form.on('submit(adduser)', function (data) {

            var load = layer.load();
            $.ajax({
                url: "{:url('User/addUser')}",
                type: "POST",
                data: data.field,
                dataType: 'json',
                success: function (res) {
                    if (1 == res.code) {
                        layer.msg(res.msg, {icon: 1}, function () {
                            parent.location.reload();
                        });
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                    layer.close(load);
                }
            });
            return false;
        });
    });
</script>
{/block}